package com.ldc.security.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ldc.model.SysRoleBean;
import com.ldc.model.SysUserBean;
import com.ldc.model.SysUserRoleBean;
import com.ldc.security.model.LoginUserBean;
import com.ldc.service.RoleService;
import com.ldc.service.UserRoleService;
import com.ldc.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @auther liudc
 * @since 2022/11/13
 */
@Component
public class SecurityUtils {
    private final Logger logger = LoggerFactory.getLogger(SecurityUtils.class);
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleService userRoleService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("用户名 = {}", username);
        SysUserBean sysUserBean = findByUserName(username);
        if (Objects.isNull(sysUserBean)) {
            throw new UsernameNotFoundException("找不到该用户信～");
        }
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        List<SysUserRoleBean> userRoleModels = findByUserId(sysUserBean.getId());
        if (userRoleModels.size() > 0) {
            for (SysUserRoleBean item : userRoleModels) {
                SysRoleBean sysRoleBean = findByRoleId(item.getRoleId());
                if (sysRoleBean == null) {
                    continue;
                }
                grantedAuthorityList.add(new SimpleGrantedAuthority(sysRoleBean.getName()));
            }
        }
        return new LoginUserBean(sysUserBean, grantedAuthorityList);
    }

    //获取用户信息
    private SysUserBean findByUserName(String loginName) {
        LambdaQueryWrapper<SysUserBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserBean::getLoginName, loginName)
                .or(true)
                .eq(SysUserBean::getUserName, loginName);
        return userService.getOne(queryWrapper);
    }

    // 获取 user-role 对应表
    private List<SysUserRoleBean> findByUserId(long userId) {
        LambdaQueryWrapper<SysUserRoleBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRoleBean::getUserId, userId);
        return userRoleService.list(queryWrapper);
    }

    // 获取role
    private SysRoleBean findByRoleId(long roleId) {
        LambdaQueryWrapper<SysRoleBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleBean::getId, roleId);
        return roleService.getOne(queryWrapper);
    }
}
